05. Introduction to JUnits
Introduction to JUnits
# Introduction to JUnits
We'll now go through a hands-on example that will get you some experience testing using JUnits.
To follow along, you can download the starter code that Sareeta uses from this GitHub repository.
Getting familiar with the starter code heading
Getting familiar with the starter code
ND035 C04 L02 A08.1 Basics Of Junits-
Task Description:
Before going further, be sure that you've done the following:
Task Feedback:
Good—now we are ready to start writing some tests!
Writing JUnits for the helper methods
ND035 C04 L02 A08.2 Basics Of Junits-
Find the Maven Dependency
Task Description:
Go ahead and see if you can find the Maven dependency.
(Google is your friend!)
If you have any trouble finding it, no worries—Sareeta will show how in the next video.
Task Feedback:
Great!
ND035 C04 L02 A08.3 Basics Of Junits-
Add the HelperTest class
Task Description:
Before you move on, be sure that you've tried the following things for yourself:
Task Feedback:
Nice work!
ND035 C04 L02 A08.4 Basics Of Junits-
Writing tests on the Helper
class
ND035 C04 L02 A08.5 Basics Of Junits-
Write a test for the getMergedList
method
Task Description:
See if you can write a JUnit test (in HelperTest
) for the getMergedList
method. Once you've given it a shot, you can check out our example below to compare.
(As always, if you run into trouble and don't quite get it working, no worries—the point is simply to practice and to learn!)
Task Feedback:
Awesome!
Solution
Below is solution code for all of the tests, including getMergedList
. We encourage you to try out as many of these as you like, and compare your code with the examples. (As always, it's possible your code looks a bit different and still accomplishes the end goal!)
```
package com.udacity.examples.Testing;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import java.util.Arrays;
import java.util.List;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
public class HelperTest {
@Test
public void test() {
assertEquals("test", "test1");
}
@Test
public void test1() {
assertEquals("test", "test");
}
/**
* Need to be void and public
* @return
*
* @Test
private String test2() {
return "";
}
*/
@Test
public void validate_getCount() {
List<String> empNames = Arrays.asList("sareeta", "", "john","");
assertEquals(2, Helper.getCount(empNames));
}
@Test
public void validate_3lengthString() {
List<String> empNames = Arrays.asList("sareeta", "", "Jeff","sam");
assertEquals(2, Helper.getStringsOfLength3(empNames));
}
/*
* Part 2
* why not to use test in the name?
* Because annotations s
*/
@Test
public void verify_list_is_squared(){
List<Integer> yrsOfExperience = Arrays.asList(13,4,15,6,17,8,19,1,2,3);
List<Integer> expected = Arrays.asList(169, 16, 225, 36, 289, 64, 361, 1, 4, 9);
assertEquals(expected, Helper.getSquareList(yrsOfExperience));
}
/**
* assertFail vs asserTrue
*/
@Before
public void init() {
System.out.println("init executed");
}
@After
public void teardown() {
System.out.println("teardown executed");
}
@Test
public void verify_merged_list(){
List<String> empNames = Arrays.asList("sareeta", "", "john","");
assertEquals("sareeta, john", Helper.getMergedList(empNames));
}
@Test
public void verify_getMax(){
List<Integer> empLevel = Arrays.asList(3,3,3,5,7,2,2,5,7,5);
assertEquals(7, Helper.getStats(empLevel).getMax());
}
/**
* this should be public static
*/
@BeforeClass
public static void initClass() {
System.out.println("init Class executed");
}
@AfterClass
public static void teardownclass() {
System.out.println("teardown Class executed");
}
/**
*assertArrayEquals
*How should you debug this?
*/
@Test
public void verify_ArrayListTest(){
int[] yrsOfExperience = {13,4,15,6,17,8,19,1,2,3};
int[] expected = {13,4,15,6,17,8,19,1,2,3};
assertArrayEquals(expected, yrsOfExperience);
}
/**
* parameterized test next class
*/
}```
Quiz